import { createRouter, createWebHistory } from 'vue-router'
import { Notify } from 'vant'
const Home = () =>
  import ('../views/home/Home')
const Category = () =>
  import ('../views/category/Category')
const Profile = () =>
  import ('../views/profile/Profile')
const ShopCart = () =>
  import ('../views/shopcart/ShopCart')
const Detail = () =>
  import ('../views/detail/Detail')
const Login = () =>
  import ('../views/profile/Login')
const Register = () =>
  import ('../views/profile/Register')
const CreateOrder = () =>
  import ('../views/order/CreateOrder')
const OrderList = () =>
  import ('../views/order/OrderList')
const OrderDetail = () =>
  import ('../views/order/OrderDetail')
const Address = () =>
  import ('../views/profile/Address')
const AddressEdit = () =>
  import ('../views/profile/AddressEdit')
const About = () =>
  import ('../views/profile/About')
const Setting = () =>
  import ('../views/profile/Setting')
const Collect = () =>
  import ('../views/profile/Collect')
const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: Home, meta: { title: '图书兄弟' } },
  { path: '/category', component: Category, meta: { title: '图书兄弟-商品分类' } },
  { path: '/profile', component: Profile, meta: { title: '图书兄弟-个人中心', isAuthRequired: true } },
  { path: '/shopcart', component: ShopCart, meta: { title: '图书兄弟-购物车', isAuthRequired: true } },
  { path: '/detail', component: Detail, meta: { title: '图书兄弟-商品详情' } },
  { path: '/createorder', component: CreateOrder, meta: { title: '图书兄弟-生成订单', isAuthRequired: true } },
  { path: '/orderList', component: OrderList, meta: { title: '图书兄弟-订单列表', isAuthRequired: true } },
  { path: '/orderDetail', component: OrderDetail, meta: { title: '图书兄弟-订单详情', isAuthRequired: true } },
  { path: '/address', component: Address, meta: { title: '图书兄弟-地址管理', isAuthRequired: true } },
  { path: '/addressedit', component: AddressEdit, meta: { title: '图书兄弟-编辑地址', isAuthRequired: true } },
  { path: '/about', component: About, meta: { title: '图书兄弟-关于我们', isAuthRequired: true } },
  { path: '/setting', component: Setting, meta: { title: '图书兄弟-帐户设置', isAuthRequired: true } },
  { path: '/collect', component: Collect, meta: { title: '图书兄弟-我的收藏', isAuthRequired: true } },
  { path: '/login', component: Login, meta: { title: '图书兄弟-用户登录' } },
  { path: '/register', component: Register, meta: { title: '图书兄弟-注册页面' } }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  console.log(!window.localStorage.getItem('token'))
  if (to.meta.isAuthRequired && !window.localStorage.getItem('token')) {
    Notify('您还没有登录，请先登录')
    next('/login')
  }
  next()
  document.title = to.meta.title
})

export default router
